package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("region")
public class RegionController {
    @Autowired
    private IRegionService regionService;

    @RequestMapping("list")
    public String list() {
        return "/region/list";
    }

    @RequestMapping("listByParentId")
    @ResponseBody
    public Object listByParentId(Long parentId) {
        List<Region> list = regionService.selectByParent(parentId);
        List<Map> lists = new ArrayList<>();
        for (Region region : list) {
            lists.add(region.toTreeViewMap());
        }
        return lists;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Region entity) {
        JsonResult result = new JsonResult();
        try {
            regionService.saveOrUpdate(entity);
        } catch (Exception e) {
            result.mark("操作失败");
            e.printStackTrace();
        }
        return result;
    }
}
